package com.wyz.socket.core.base.frame.receive;

import com.wyz.socket.core.base.IoArgs;
import com.wyz.socket.core.base.frame.send.SendHeaderFrame;

public class ReceiveHeaderFrame extends AbstractReceiveFrame {

    private final byte[] body;

    public ReceiveHeaderFrame(byte[] header) {
        super(header);
        this.body = new byte[bodyRemaining];
    }

    @Override
    protected int consumeBody(IoArgs args) {
        int offset = body.length - bodyRemaining;
        return args.writeTo(body, offset, bodyRemaining);
    }

    // 00000000  00000000  00000000  00000000  00000000
    // body 0    body 1    body 2    body 3    body 4
    public int getPacketLength() {
        return ((((int) body[1]) & 0xff) << 24) | ((((int) body[2]) & 0xff) << 16) | ((((int) body[3]) & 0xff) << 8) | ((((int) body[4]) & 0xff));
    }

    public byte getPacketType(){
        return body[5] ;
    }

    public byte[] getPacketInfo(){
        if(body.length > SendHeaderFrame.PACKET_HEADER_FRAME_MIN_LENGTH){
            byte[] headerinfo = new byte[body.length - SendHeaderFrame.PACKET_HEADER_FRAME_MIN_LENGTH] ;
            System.arraycopy(body , SendHeaderFrame.PACKET_HEADER_FRAME_MIN_LENGTH , headerinfo , 0 , headerinfo.length);
            return headerinfo ;
        }
        return null ;
    }
}
